﻿#include "evaluate.h"
#include "ui_evaluate.h"
#include <QDebug>
#include <QButtonGroup>
#include "userDataFormate.h"
#include "currentuserdata.h"
#pragma execution_character_set("utf-8")
evaluate::evaluate(QWidget *parent):
    QWidget(parent),
    ui(new Ui::evaluate),
    timer(NULL),
    counter(0),
    testTimer(NULL),
    scope(0),
    evaluateState(false)
{
    ui->setupUi(this);

    ui->reset_pushButton->setVisible(false);
    QButtonGroup * buttonGroup = new QButtonGroup;
    // 设置是否互斥
    buttonGroup->setExclusive(true);
    // 将需要互斥的按钮全部添加到 QButtonGroup 中
    buttonGroup->addButton(ui->oneGreas_Btn);
    buttonGroup->addButton(ui->twoGreas_Btn);
    buttonGroup->addButton(ui->threeGreas_Btn);
    buttonGroup->addButton(ui->fourGreas_Btn);
    buttonGroup->addButton(ui->fiveGreas_Btn);
    buttonGroup->addButton(ui->sixGreas_Btn);
    buttonGroup->setExclusive(true);

    this->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint);
    m_evaluateReport = new evaluationReport;

    timer = new QTimer();
    timer->setInterval(1000);
    connect(timer,&QTimer::timeout,this,&evaluate::slotTimer);

    testTimer = new QTimer();
    testTimer->setInterval(50);

    ui->leftCountDown_Label->setObjectName("leftLabel");
    ui->rightCountDown_Label->setObjectName("rightLabel");
    ui->upCountDown_Label->setObjectName("upLabel");
    ui->downCountDown_Label->setObjectName("downLabel");

    connect(ui->leftCountDown_Label,SIGNAL(signalTheEnd()),this,SLOT(slotDirection()));
    connect(ui->rightCountDown_Label,SIGNAL(signalTheEnd()),this,SLOT(slotDirection()));
    connect(ui->upCountDown_Label,SIGNAL(signalTheEnd()),this,SLOT(slotDirection()));
    connect(ui->downCountDown_Label,SIGNAL(signalTheEnd()),this,SLOT(slotDirection()));

    LabelflagList << false << false <<false <<false;
}

evaluate::~evaluate()
{
    delete ui;
}
//显示倒计时
void evaluate::controlValueShow(int value,int direction)
{
    if(0 == scope)
        return;
    //0~6°范围
    else if(6 == scope)
    {
        switch(direction)
        {
        case 0:
            ui->left_ProgressBar->setValue(value);
            break;
        case 1:
            ui->right_ProgressBar->setValue(value);
            break;
        case 2:
            ui->up_ProgressBar->setValue(value);
            break;
        case 3:
            ui->down_ProgressBar->setValue(value);
            break;
        }
    }
    //6~12°范围
    else if(12 == scope)
    {
        switch(direction)
        {
        case 0:
            ui->left_ProgressBar->setValue(value);
            break;
        case 1:
            ui->right_ProgressBar->setValue(value);
            break;
        case 2:
            ui->up_ProgressBar->setValue(value);
            break;
        case 3:
            ui->down_ProgressBar->setValue(value);
            break;
        }
    }
}


void evaluate::on_start_pushButton_clicked()
{

    if(ui->start_pushButton->text() == tr("开始"))
    {
        pointList.clear();

        evaluateState = true;
        ui->start_pushButton->setText(tr("完成"));
        ui->reset_pushButton->setVisible(true);
        timer->start();
        testTimer->start();
        counter = 0;
        st_evaluatereport.reportDate = QDateTime::currentDateTime();
    }
    else if(ui->start_pushButton->text() == tr("完成"))
    {
        evaluateState = false;

        QPixmap backPixmap = ui->groupBox->grab();
        //创建文件名
        QString fileName = "./createFile/evaluateReport/tempPicture/evaluatePix.png";
        if(!backPixmap.save(fileName))
            qDebug()<<"评估图片保存失败";
        else
            st_evaluatereport.tempBackPixmapPath = fileName;

        ui->start_pushButton->setText(tr("开始"));
        ui->reset_pushButton->setVisible(false);
        timer->stop();
        testTimer->stop();
        st_patientMsg =  CurrentUserData::getInstace()->getCurrentPatientMsg();
        st_evaluatereport.ID = st_patientMsg.ID;
        st_evaluatereport.name = st_patientMsg.userName;
        st_evaluatereport.sex = st_patientMsg.sex;
        st_evaluatereport.phoneNumber = st_patientMsg.userPhone;
        st_evaluatereport.height = st_patientMsg.height;
        st_evaluatereport.age = st_patientMsg.age;
        st_evaluatereport.remark = st_patientMsg.remarkMessage;
        //阻力
        if(ui->oneGreas_Btn->isChecked())
            st_evaluatereport.resistance=1;
        if(ui->twoGreas_Btn->isChecked())
            st_evaluatereport.resistance=2;
        if(ui->threeGreas_Btn->isChecked())
            st_evaluatereport.resistance=3;
        if(ui->fourGreas_Btn->isChecked())
            st_evaluatereport.resistance=4;
        if(ui->fiveGreas_Btn->isChecked())
            st_evaluatereport.resistance=5;
        if(ui->sixGreas_Btn->isChecked())
            st_evaluatereport.resistance=6;

        st_evaluatereport.evaluateDuration=counter;//单位为s
        st_evaluatereport.frontMaxAngle=ui->frontAngle_label->text().toInt();

        st_evaluatereport.backMaxAngle=ui->backAngle_label->text().toInt();
        st_evaluatereport.leftMaxAngle=ui->leftAngle_label->text().toInt();
        st_evaluatereport.rightMaxAngle=ui->rightAngle_label->text().toInt();
        counter = 0;
        m_evaluateReport->setReportData(st_evaluatereport,1,pointList);
        m_evaluateReport->show();
    }
}

void evaluate::slotTimer()
{
    ++counter;
    int min = counter/60;
    int sec = counter%60;
    ui->time_label->setText(QString("%1 min  %2 s").arg(min).arg(sec));
}

void evaluate::on_reset_pushButton_clicked()
{
    for(int i = 0;i < LabelflagList.size();i++)
        LabelflagList[i] = false ;

    ui->left_ProgressBar->setValue(0);
    ui->right_ProgressBar->setValue(0);
    ui->up_ProgressBar->setValue(0);
    ui->down_ProgressBar->setValue(0);

    ui->leftAngle_label->setText("");
    ui->rightAngle_label->setText("");
    ui->frontAngle_label->setText("");
    ui->backAngle_label->setText("");

}


void evaluate::slotDirection()
{
    QString senderStr = sender()->objectName();

    if(senderStr == "leftLabel")
    {
        //采集完数据
        ui->leftCountDown_Label->setShow(false);
        LabelflagList[0] = true;
        int value = ui->left_ProgressBar->value();
        ui->leftAngle_label->setText(QString::number(value));
    }
    else if(senderStr == "rightLabel")
    {
        ui->rightCountDown_Label->setShow(false);
        LabelflagList[1] = true;
        int value = ui->right_ProgressBar->value();
        ui->rightAngle_label->setText(QString::number(value));
    }
    else if(senderStr == "upLabel")
    {
        ui->upCountDown_Label->setShow(false);
        LabelflagList[2] = true;
        int value = ui->up_ProgressBar->value();
        ui->frontAngle_label->setText(QString::number(value));
    }
    else if(senderStr == "downLabel")
    {
        ui->downCountDown_Label->setShow(false);
        LabelflagList[3] = true;
        int value = ui->down_ProgressBar->value();
        ui->backAngle_label->setText(QString::number(value));
    }
}

void evaluate::slotReceiveData(ST_RealParam st_realParam)
{
    if(evaluateState)
    {

        float xAngle = st_realParam.xAngle;
        float yAngle = st_realParam.yAngle;
        QPoint point(xAngle,yAngle);
        pointList.append(point);
        if(qAbs(xAngle) >= qAbs(yAngle))
        {
            value = xAngle;
            //x正向
            if(xAngle > 0)
            {
                if(!LabelflagList[1])
                {
                    ui->right_ProgressBar->setValue(xAngle);
                    if(qAbs(value) >2)
                    {
                        rightPointList.append(-value);
                        if(rightPointList.size() > 2)
                        {
                            int slope = rightPointList.last() - rightPointList.first();
                            if(slope == 0 && value != 0 && !LabelflagList[1])
                            {
                                if(!ui->rightCountDown_Label->isVisible())
                                    ui->rightCountDown_Label->setShow(true);
                            }
                            else
                            {
                                if(ui->rightCountDown_Label->isVisible())
                                    ui->rightCountDown_Label->setShow(false);
                            }
                            rightPointList.clear();
                        }
                    }
                }

            }
            //x负向
            else
            {
                if(!LabelflagList[0])
                {
                    ui->left_ProgressBar->setValue(-xAngle);
                    if(qAbs(value) >2)
                    {
                        leftPointList.append(-value);
                        if(leftPointList.size() > 2)
                        {
                            int slope = leftPointList.last() - leftPointList.first();
                            if(slope == 0 && value != 0 && !LabelflagList[0])
                            {
                                if(!ui->leftCountDown_Label->isVisible())
                                    ui->leftCountDown_Label->setShow(true);
                            }
                            else
                            {
                                if(ui->leftCountDown_Label->isVisible())
                                    ui->leftCountDown_Label->setShow(false);
                            }
                            leftPointList.clear();
                        }
                    }
                }
            }
        }
        //y方向
        else
        {
            value = yAngle;
            //y正向
            if(yAngle > 0)
            {
                if(!LabelflagList[2])
                {
                    ui->up_ProgressBar->setValue(yAngle);
                    if(qAbs(value) >2)
                    {
                        upPointList.append(-value);
                        if(upPointList.size() >2)
                        {
                            int slope = upPointList.last() - upPointList.first();
                            if(slope == 0 && value != 0 && !LabelflagList[2])
                            {
                                if(!ui->upCountDown_Label->isVisible())
                                    ui->upCountDown_Label->setShow(true);
                            }
                            else
                            {
                                if(ui->upCountDown_Label->isVisible())
                                    ui->upCountDown_Label->setShow(false);
                            }
                            upPointList.clear();
                        }
                    }
                }
            }
            //y负向
            else
            {
                if(!LabelflagList[3])
                {
                    ui->down_ProgressBar->setValue(-yAngle);
                    if(qAbs(value) >2)
                    {
                        downPointList.append(-value);
                        if(downPointList.size() > 2)
                        {
                            int slope = downPointList.last() - downPointList.first();
                            if(slope == 0 && value != 0 && !LabelflagList[3])
                            {
                                if(!ui->downCountDown_Label->isVisible())
                                    ui->downCountDown_Label->setShow(true);
                            }
                            else
                            {
                                if(ui->downCountDown_Label->isVisible())
                                    ui->downCountDown_Label->setShow(false);
                            }
                            downPointList.clear();
                        }
                    }
                }

            }

        }

    }
}


void evaluate::changeEvent(QEvent* event)
{
    switch (event->type())
    {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        QWidget::changeEvent(event);
        break;
    }
}


